//
//  ViewController.m
//  Supermarket
//
//  Created by qskj-ios on 2018/12/10.
//  Copyright © 2018年 com.qiansheng. All rights reserved.
//

#import "ViewController.h"
#import "CommonViewController.h"
#import "OffsetListViewController.h"
#import "LeaseListViewController.h"
#import "BookListViewController.h"

@interface ViewController ()<UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

- (IBAction)didClickedLoginButton:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    self.nameTextField.text = @"zkd";
//    self.passwordTextField.text = @"123456";
    self.navigationController.delegate = self;
    self.nameTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.passwordTextField.clearButtonMode = UITextFieldViewModeAlways;
    
    [self.nameTextField customBorderWithSize:kSeparatorHeight andPosition:UIViewBorderPositionBottom andColor:kSeparatorColor andLeadingInset:0 andTrailingInset:0];
    [self.passwordTextField customBorderWithSize:kSeparatorHeight andPosition:UIViewBorderPositionBottom andColor:kSeparatorColor andLeadingInset:0 andTrailingInset:0];
}

- (IBAction)didClickedLoginButton:(id)sender {
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:self.nameTextField.text forKey:@"user_name"];
    [parameters setObject:self.passwordTextField.text forKey:@"user_pwd"];

    [SVProgressHUD showWithStatus:@"登录中..."];
    WEAKSELF
    [NetworkHelper apiPost:@"manager/login" parameters:parameters completion:^(id info, NSError *error) {
        [SVProgressHUD dismiss];
        if (!error) {
            NSInteger sta = [[info objectForKey:@"sta"] integerValue];
            NSString *remarks = [info objectForKey:@"remarks"];
            if (sta == 1) {
                NSString *do_id = [[info objectForKey:@"response"] objectForKey:@"do_id"];
                [SupermarketUtil saveDataWithKey:@"do_id" ofValue:do_id];
    
                CommonViewController *dvc = [[CommonViewController alloc] init];
                [weakSelf.navigationController pushViewController:dvc animated:YES];
        
            } else if (sta == 0) {
                [SVProgressHUD showErrorWithStatus:remarks];
            }
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - UINavigationControllerDelegate

/** 隐藏导航栏 **/
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated {
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

@end
